//
//  LSLeagueCompetitionBaseVController.m
//  LookScore
//
//  Created by wr on 2018/1/31.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSLeagueCompetitionBaseVController.h"
#import "LSLeagueHomePageVController.h"//联赛主页
#import "LSLeagueTableViewController.h"//联赛积分榜
#import "LSLeagueFixturesViewController.h"//联赛赛程
#import "LSCupTableFixturesViewController.h"//杯赛赛程积分榜
#import "LSLeagueHandicapViewController.h"//联赛让球盘
#import "LSLeagueSizeplateViewController.h"//联赛大小盘
#import "LSLeagueScorerViewController.h"//联赛射手榜
#import "LSPulldDownOptionsView.h"//下拉view
#import "ZJScrollPageView.h"

@interface LSLeagueCompetitionBaseVController ()<ZJScrollPageViewDelegate>

@property(strong, nonatomic)NSArray<NSString *> *titles;

@property(strong, nonatomic)NSArray<UIViewController<ZJScrollPageViewChildVcDelegate> *> *childVcs;

@property (nonatomic, strong) ZJScrollPageView *scrollPageView;

@property(strong,nonatomic)UIButton *navigetionBu;

@property(strong,nonatomic)LSPulldDownOptionsView *pullView;


@end

@implementation LSLeagueCompetitionBaseVController{
    LSLeagueHomePageVController *homeVC;
    LSLeagueTableViewController *tableVC;
    LSLeagueFixturesViewController *fixturesVC;
    LSCupTableFixturesViewController *tableFixturesVC;
    LSLeagueHandicapViewController *handicapVC;
    LSLeagueSizeplateViewController *sizeplateVC;
    LSLeagueScorerViewController *scorerVC;
    
    NSString *leagueId;
    NSString *year;
    NSString *leagueType;
    NSDictionary *homePageDic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = LSTitleWhiteColor;
    
    leagueId = self.leagueInfoDic[@"LeagueId"];
    year = [self.leagueInfoDic[@"LeagueHistory"] componentsSeparatedByString:@","].firstObject;
    leagueType = self.leagueInfoDic[@"LeagueType"];
    
    
//    [self loadLeagueHomePageData];
    
    [self addScrollerUI];
    
    MyLog(@"leagueDic -- %@",self.leagueInfoDic);
}
- (void)loadLeagueHomePageData{
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    //    hud.label.text=@"正在请求中...";
    hud.mode=MBProgressHUDModeIndeterminate;
    MyLog(@"leagueId --- %@ /n season --- %@",leagueId,year);
    [LSHttpEnclosureTool GET:LSLeagueDetail parameters:@{@"leagueId":leagueId,@"year":year} resBlock:^(id responseObject) {
        
        if (responseObject) {
            MyLog(@"leagueHomePage ---- %@",responseObject);
            homePageDic = responseObject;
            [hud hideAnimated:YES];
            [self addScrollerUI];
            
        }
        
    }];
}

#pragma mark 🔥 Event

-(void)chooseTimerEvent:(UIButton *)sender{
    
    NSArray * dataArray = [self.leagueInfoDic[@"LeagueHistory"] componentsSeparatedByString:@","];
    
    NSMutableArray *array = dataArray.mutableCopy;
    MyLog(@"LeagueHistory --- %@",array);
    if (!self.pullView) {
        LSPulldDownOptionsView *pullView=[[LSPulldDownOptionsView alloc]initWithFrame:CGRectMake(0, 0, LSScreenWidth, 35*array.count) normalTitleColor:LSNavMainColor selectedTitleColor:LSTitleWhiteColor normalBackViewColor:LSTitleWhiteColor selectedBackViewColor:LSNavMainColor height:35*array.count];
     
        pullView.modelArray=array.mutableCopy;
        pullView.cellClickBlock = ^(NSInteger index) {
             [self.navigetionBu setTitle:array[index] forState:UIControlStateNormal];
//            homeVC.update = array[index];
            tableFixturesVC.update = array[index];
            tableVC.update = array[index];
            fixturesVC.update = array[index];
            handicapVC.update = array[index];
            sizeplateVC.update = array[index];
            scorerVC.update = array[index];
        };
        self.pullView=pullView;
        [self.view addSubview:pullView];
    }
    self.pullView.hidden=NO;
    
}





#pragma mark 🔥 scrollerUI <ZJScrollPageViewDelegate>
-(void)addScrollerUI{    
    NSArray *yearArray = [self.leagueInfoDic[@"LeagueHistory"] componentsSeparatedByString:@","];
    UIButton *navigetionBu=[UIButton new];
    [navigetionBu setTitleColor:LSNavTitlenColor forState:UIControlStateNormal];
    [navigetionBu setTitle:yearArray.firstObject forState:UIControlStateNormal];
    [navigetionBu setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    navigetionBu.size = CGSizeMake(150, 64);
//    navigetionBu.titleEdgeInsets=UIEdgeInsetsMake(0,-50, 0, 50);
//    navigetionBu.imageEdgeInsets=UIEdgeInsetsMake(0, 60, 0, -60);
    [navigetionBu addTarget:self action:@selector(chooseTimerEvent:) forControlEvents:UIControlEventTouchUpInside];
    self.navigetionBu=navigetionBu;
    self.navigationItem.titleView=navigetionBu;
  
    UIButton *rightButton=[UIButton new];
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightButton setTitle:@"赛制" forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:rightButton];
    
    self.childVcs=[self setupChildVc];
    
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    //显示滚动条
    style.showLine = YES;
    // 颜色渐变
    style.gradualChangeTitleColor = YES;
    
    style.scrollLineColor=LSNavMainColor;
    
    style.segmentHeight=35;
    
    style.autoAdjustTitlesWidth=YES;
    
    style.normalTitleColor=[UIColor colorWithRed:255.f/255.f green:255.f/255.f blue:255.f/255.f alpha:1];
    
    style.selectedTitleColor=LSNavTitlenColor;
    
    style.scrollLineColor=LSNavTitlenColor;
    
    
    if ([self.leagueInfoDic[@"LeagueType"] isEqualToString:@"2"]) {
        self.titles = @[@"赛程积分榜",@"让球盘",@"大小盘",@"射手榜"];
    }else{
        self.titles = @[@"主页",@"积分榜",@"让球盘",@"大小盘",@"赛程"];
    }
        
    // 初始化
    _scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,self.view.bounds.size.height) segmentStyle:style titles:self.titles parentViewController:self delegate:self];
    _scrollPageView.segmentView.backgroundColor=LSNavMainColor;
    [self.view addSubview:_scrollPageView];
}
- (NSArray *)setupChildVc {
    
    
    
//    fixturesVC.rounding = [NSString stringWithFormat:@"%@",homePageDic[@"Rounding"]];
//    fixturesVC.totalRound = [NSString stringWithFormat:@"%@",homePageDic[@"TotalRound"]];
    
    
    
    
    handicapVC = [LSLeagueHandicapViewController new];
    handicapVC.leagueId = leagueId;
    handicapVC.season = year;
    
    
    sizeplateVC = [LSLeagueSizeplateViewController new];
    sizeplateVC.leagueId = leagueId;
    sizeplateVC.season = year;
    
    
   
    
    NSArray *childVcs;
    if ([self.leagueInfoDic[@"LeagueType"] isEqualToString:@"2"]) {
        
        tableFixturesVC = [LSCupTableFixturesViewController new];
        tableFixturesVC.leagueId = leagueId;
        tableFixturesVC.season = year;
        tableFixturesVC.leagueType = [leagueType integerValue];
        
        scorerVC = [LSLeagueScorerViewController new];
        scorerVC.leagueId = leagueId;
        scorerVC.season = year;
        
        childVcs = [NSArray arrayWithObjects:tableFixturesVC,handicapVC,sizeplateVC, scorerVC, nil];
    }else{
        
        homeVC = [LSLeagueHomePageVController new];
        homeVC.leagueId = leagueId;
        homeVC.season = year;
        //    homeVC.homePageData = homePageDic;
        
        tableVC = [LSLeagueTableViewController new];
        tableVC.leagueId = leagueId;
        tableVC.season = year;
        
        
        fixturesVC = [LSLeagueFixturesViewController new];
        fixturesVC.leagueId = leagueId;
        fixturesVC.season = year;
        
        childVcs = [NSArray arrayWithObjects:homeVC,tableVC,handicapVC,sizeplateVC, fixturesVC, nil];
    }
    
    return childVcs;
}
- (NSInteger)numberOfChildViewControllers {
    return self.titles.count;
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    
    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
    
    if (!childVc) {
        childVc = self.childVcs[index];
    }
    
    return childVc;
}


- (BOOL)shouldAutomaticallyForwardAppearanceMethods {
    return NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
